package fr.ants.controller.command;

import java.io.File;

import javax.swing.JFileChooser;

import fr.ants.controller.clock.Clock;
import fr.ants.model.stats.SimulatorStatistics;
import fr.ants.model.stats.StatsToCsv;
import fr.ants.view.component.CsvFileFilter;

/**
 * Create the stats.
 * 
 * @author Guillaume Ruscassie
 * 
 */
public class StatsCommand implements Command {

	/**
	 * Clock;
	 */
	private final Clock clock;

	/**
	 * Build.
	 * 
	 * @param clock
	 *            clock.
	 */
	public StatsCommand(final Clock clock) {
		this.clock = clock;
	}

	@Override
	public void execute() {
		JFileChooser chooser = new JFileChooser();
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		chooser.setAcceptAllFileFilterUsed(false);
		chooser.setFileFilter(new CsvFileFilter());
		// open the file chooser
		Integer returnVal = chooser.showOpenDialog(null);

		// check if the person has select a xml file
		if (returnVal != null && returnVal == JFileChooser.APPROVE_OPTION) {
			SimulatorStatistics simulatorStatistics = clock.getSimulator()
					.getStats();

			StatsToCsv.export(simulatorStatistics, chooser.getSelectedFile()
					.getAbsolutePath() + File.separator);
		}
	}

}
